package com.scu.qingshi.infrastructure.utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author qingshi
 * @Classname RedisUtil
 * @Description
 * @Data 2021/4/28
 * @Created by qingshi
 */
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public void setString(String key,String str){
        redisTemplate.opsForValue().set(key,str);
    }

    public void setStringExpire(String key, String str,Long time, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,str,time,timeUnit);
    }

    public String getString(String key){
        return (String) redisTemplate.opsForValue().get(key);
    }

    public void setObject(String key,Object o){
        redisTemplate.opsForValue().set(key, o);
    }

    public void setObjectExpire(String key, Object o,Long time, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,o,time,timeUnit);
    }

    public Object getObject(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public void deleteByKey(String key){
        redisTemplate.delete(key);
    }
}